package ms.maplestory.boot.service;

import ms.maplestory.boot.client.character.CharStat;
import lombok.RequiredArgsConstructor;
import ms.maplestory.boot.client.character.ExtendSP;
import ms.maplestory.boot.enums.JobType;
import ms.maplestory.boot.repository.CharStatRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service
@RequiredArgsConstructor
public class CharStatService extends JpaService<CharStat, Integer> {

    private final CharStatRepository charStatRepository;
    private final ExtendSPService extendSPService;

    @Override
    public CharStatRepository getRepository() {
        return charStatRepository;
    }

    @Transactional
    public CharStat createCharStat(String name, int job, byte gender, int face, int hair, int skin,
                                   int curSelectedRace, short curSelectedSubJob) {
        CharStat charStat = CharStat.builder()
                .name(name)
                .job(job)
                .gender(gender)
                .skin(skin)
                .face(face)
                .hair(hair)
                .subJob(curSelectedSubJob)
                .build();

        Set<ExtendSP> extendSP = extendSPService.createExtendSP(7, charStat);
        charStat.setExtendSP(extendSP);
        if (curSelectedRace == JobType.LoginJob.Explorer.getJobType()) {
            charStat.setSubJob(curSelectedSubJob);
        }
        save(charStat);
        return charStat;
    }
}
